<?php
namespace app\admin\controller;
use think\Db;
class Sms extends Base
{
    public function lists(){
        //ajax查询数据
        if( is_post() ){
            $cur_page     = input('page');
            $status   = input('status');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');

            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    sms_history
                ";
            $param = [];
            $w = ['where'];
            if ( $status != '100all' ) {
                $w[] = " and status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $w[] = " and (content like ? or phone like ?)";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $w[] = " and create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $w[] = " and create_time <= ?";
                $param[] = strtotime($max_time);
            }
            #条件判断
            if (count($w)>1){
              $w[1] = str_replace("and","",$w[1]);
              $sql .= implode(" ",$w);
            }
            
            #分页
            $page_size = c('a.page_size');
            $page_html = com_list_page($sql, $param, $cur_page,$page_size);

            #内容
            $start = ($cur_page - 1) * $page_size;
            $field = "*";
            $sql_list = str_replace("[1]",$field,$sql);
            $sql_list .= " order by id desc limit {$start},{$page_size}";

            $rslist = Db::query($sql_list, $param);
            foreach ($rslist as $k => $v) {
                $rslist[$k]['content']        =  com_substr($v['content'], 60);
                $rslist[$k]['status']         =  c("type.sms_status",$v['status']);
                $rslist[$k]['create_time']  = com_formate_time($v['create_time']);
                $rslist[$k]['update_time']  = com_formate_time($v['update_time']);
            }
            $body_html = com_list_body($rslist, ["id","phone", "code","content","nums","create_time", "status","update_time"]);
            com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html]]);
        }
        $data = [
            'status'    => ['100all'=>'全部状态'] + c('type.sms_status'),
        ];
        return view('', $data);
    }
}
